package boxe.timer.view;

/**
 * Main window with the timer, the command buttons and menu bar.
 * 
 * @author Julien Perriard
 * @date 18.05.2010
 *
 */

	
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;

import boxe.timer.env.Propertie;
import boxe.timer.manager.ResizeManager;
import boxe.timer.manager.TimerController;



public class MainWindow extends JFrame{
	

	//variable local


	
	//graphical components
	private JPanel panTop,panMaster,panUp,panLeft,panCenter;
	public JPanel panTimer, panTitle;
	private JPanel panRound;
	public JLabel lblLatance, lblTitle;
	public JLabel lblDurRound;
	public JLabel lblDurPause;
	public JLabel lblEndRound;
	public JLabel lblNbrRound;
	public JLabel lblTimer;
	public JLabel lblRound;
	private JButton butStart,butPause,butStop;
	private JMenuBar barMenu;
	private JMenu menuFiles,menuConfig,menuAbout;
	private JMenuItem itemLoad,itemSave,itemClose;
	private JMenuItem itemAbout, itemModeManager;
	private Font roundFont,timerFont;
	/*
	 * Resize manager
	 */
	ResizeManager resize;
	
	

	public MainWindow(TimerController timer) {
	
	
	/*container--------------------------------*/
	panTop = new JPanel(new BorderLayout());
	panMaster = new JPanel(new BorderLayout());
	panLeft = new JPanel(new FlowLayout(FlowLayout.LEFT));
	panCenter = new JPanel(new FlowLayout(FlowLayout.CENTER));
	panTimer = new JPanel(new FlowLayout(FlowLayout.CENTER));
	panRound = new JPanel(new FlowLayout(FlowLayout.RIGHT));
	panTitle = new JPanel();
	lblTitle = new JLabel(" ");
	lblTitle.setFont(Propertie.TITLEFONT);
	Box[] box = new Box[1];
	box[0] = Box.createHorizontalBox();	
	
	/*menu--------------------------------*/
	barMenu = new JMenuBar();
	menuFiles = new JMenu("Files");
	menuConfig = new JMenu("Config");
	menuAbout = new JMenu("?");
	itemLoad = new JMenuItem("Load config");
	itemSave = new JMenuItem("Save config");
	itemClose = new JMenuItem("Close");
	itemAbout = new JMenuItem("About");
	itemModeManager = new JMenuItem("Mode Manager");
	
	//add menu to frame
	menuFiles.add(itemLoad);
	menuFiles.add(itemSave);
	menuFiles.add(itemClose);
	menuConfig.add(itemModeManager);
	menuAbout.add(itemAbout);
	barMenu.add(menuFiles);
	barMenu.add(menuConfig);
	barMenu.add(menuAbout);
	
	/*button------------------------------*/
	ImageIcon iconStart = new ImageIcon(Propertie.ICON_START);
	ImageIcon iconPause = new ImageIcon(Propertie.ICON_HOLD);
	ImageIcon iconStop = new ImageIcon(Propertie.ICON_STOP);
	butStart = new JButton(iconStart);
	butPause = new JButton(iconPause);
	butStop = new JButton(iconStop);
	butStart.setPreferredSize(new Dimension(100,50));
	butPause.setPreferredSize(new Dimension(100,50));
	butStop.setPreferredSize(new Dimension(100,50));
	

	box[0].add(butStart);
	box[0].add(butPause);
	box[0].add(butStop);
	
	/*label number Round----------------*/
	lblRound = new JLabel("00");
	lblRound.setFont(Propertie.ROUNDFONT);
	
	/*timer------------------------------*/
	lblTimer = new JLabel("00:00");
	lblTimer.setFont(Propertie.TIMERFONT);
	panTimer.setBackground(Color.white);
	panTimer.setBorder(BorderFactory.createBevelBorder(
			BevelBorder.LOWERED,Color.black,Color.darkGray));
	
	/*actions----------------------------------*/
	itemLoad.setActionCommand(Propertie.LOAD);
	itemSave.setActionCommand(Propertie.SAVE);
	itemClose.setActionCommand(Propertie.CLOSE);
	itemAbout.setActionCommand(Propertie.ABOUT);
	itemModeManager.setActionCommand(Propertie.MODE_MANAGER);
	itemLoad.addActionListener(timer);
	itemSave.addActionListener(timer);
	itemClose.addActionListener(timer);;
	itemAbout.addActionListener(timer);
	itemModeManager.addActionListener(timer);
	
	butStart.setActionCommand(Propertie.ST_START);
	butPause.setActionCommand(Propertie.ST_HOLD);
	butStop.setActionCommand(Propertie.ST_STOP);
	butStart.addActionListener(timer);
	butPause.addActionListener(timer);
	butStop.addActionListener(timer);
	
	/*add panel------------------------------*/
	panTimer.add(lblTimer);
	panTitle.add(lblTitle);
	panLeft.add(box[0]);
	panRound.add(lblRound);
	//panTop.add(panImage, BorderLayout.CENTER);
	panTop.add(panRound, BorderLayout.EAST);
	panTop.add(panLeft, BorderLayout.WEST);
	panMaster.add(panTitle,BorderLayout.NORTH);
	panMaster.add(panTop,BorderLayout.CENTER);
	panMaster.add(panTimer,BorderLayout.SOUTH);
	this.setJMenuBar(barMenu);
	this.getContentPane().add(panMaster);
	
	
	/*frame--------------------------------*/
	this.setSize(Propertie.TIMER_WIDTH,Propertie.TIMER_HEIGHT);
	this.setIconImage(new ImageIcon(Propertie.ICON).getImage());
	this.setResizable(true);
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	//center
	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	Dimension frameSize = this.getSize();
	this.setLocation(
		(screenSize.width - frameSize.width)/2,
		(screenSize.height-frameSize.height)/2
	);
	
	/*
	 * Component listener
	 */
	resize = new ResizeManager(this);
	this.addComponentListener(resize);
	//look and feel
	try {
		UIManager.setLookAndFeel(
				UIManager.getSystemLookAndFeelClassName());
		SwingUtilities.updateComponentTreeUI(this);
	}catch (Exception e) {}
	//this.pack();
	this.setVisible(true);
	
	}
	
	/**
	 * Set the mode and run title
	 * 
	 * @param title
	 */
	public void setTxtTitle(String title) {
		this.lblTitle.setText(title);
	}
	
	/**
	 * Getters and Setters
	 */
	public JPanel getPanTimer() {
		return panTimer;
	}

	public void setPanTimer(JPanel panTimer) {
		this.panTimer = panTimer;
	}

	public JLabel getLblTitle() {
		return lblTitle;
	}

	public void setLblTitle(String title) {
		lblTitle.setText(title);
	}

	public JLabel getLblCountRound() {
		return lblRound;
	}

	public void setLblCountRound(JLabel lblCountRound) {
		this.lblRound = lblCountRound;
	}

	public Font getTimerFont() {
		return timerFont;
	}

	public void setTimerFont(Font timerFont) {
		this.timerFont = timerFont;
	}

	public JLabel getLblTimer() {
		return lblTimer;
	}

	public void setLblTimer(JLabel lblTimer) {
		this.lblTimer = lblTimer;
	}
}
